<?php

namespace App\Service\Third;

use App\Helpers\AliyunUtil;
use App\Helpers\ImUtil;
// use App\Helpers\ShumeiUtil;
// use App\Model\BlackWords;
// use App\Model\Message;
use App\Helpers\RedisUtil;
use App\Model\Service\ConsumeService;
use App\Model\Service\NewGirlReplyPackageService;
use App\Model\Setting\TmpSetting;
// use App\Model\User;
use App\Model\User;
use App\Service\BaseService;
use Hyperf\AsyncQueue\Driver\DriverFactory;

/**
 * 融云消息回调-消息检测（敏感词等）
 */
class RongCheckmsgService extends BaseService
{
    //过审
    protected $passVrfy = 1;
    //直接放行
    protected $passGo = 2;

    function __construct(DriverFactory $driverFactory)
    {
        parent::__construct($driverFactory);
        $this->queueDriver = $driverFactory->get('default');
    }

    public function response()
    {

        //不过滤消息
        if (!TmpSetting::getMsgFilter()) return $this->responseJson(true, $this->passGo);
        $callbackInfo = $this->request->all();
//        $userInfo = User::where('im_account',$callbackInfo['fromUserId'])->select('app_version')->first();
//        if(!empty($userInfo) && version_compare($userInfo->app_version,'1.4.5','<') ) return $this->responseJson(true, $this->passGo);

        //放行 - 服务端消息直接
        if ($callbackInfo['os'] == 'Server') return $this->responseJson(true, $this->passGo);

        //放行 - 小助手的消息
        $imAccount = config('busi.xiaozhushouImAccount');
        if (in_array($imAccount, [$callbackInfo['fromUserId'], $callbackInfo['targetId']])) {
            return $this->responseJson(true, $this->passGo);
        }

        //客服账号
        $customerService = config('busi.customerService');
        if(in_array($callbackInfo['fromUserId'],$customerService) || in_array($callbackInfo['targetId'],$customerService)){
            return $this->responseJson(true, $this->passGo);
        }

        //视频放行
        if($callbackInfo['msgType'] == 'JT:USER_VIDEO'){
//            ConsumeService::setConsume($callbackInfo['fromUserId'],$callbackInfo['targetId']);
            return $this->responseJson(true, $this->passGo);
        }

        //放行 - 自动打招呼消息
        $content = json_decode($callbackInfo['content'], true);
        if (isset($content['user']['extra']) && $content['user']['extra']) {
            $extra = json_decode($content['user']['extra'], true);
            if (isset($extra['auto_msg']) && $extra['auto_msg'] == 1) {
//                ConsumeService::setConsume($callbackInfo['fromUserId'],$callbackInfo['targetId']);
                return $this->responseJson(true, $this->passGo);
            }
        }

        //判断是否是系统消息
        if(isset($content['is_system']) && $content['is_system'] == 1){
//            ConsumeService::setConsume($callbackInfo['fromUserId'],$callbackInfo['targetId']);
            return $this->responseJson(true, $this->passGo);
        }

        //记录日志
        check_dev_env() && log_info(__CLASS__ . "==" . json_encode($content));
        switch ($callbackInfo['msgType']) {
            case ImUtil::RONG_TEXT_TYPE:
                return $this->responseJson(true, $this->passVrfy);
            case ImUtil::RONG_IMG_NEW:
                return $this->responseImageJson(true, $this->passVrfy);
        }

        return $this->responseJson(true, $this->passVrfy);
    }

    /**
     * 返回指定信息
     */
    function responseJson($legal = true, $pass = 1)
    {
        return [
            'pass' => $pass,
            'replaceContent' => $legal ? '' : json_encode([
                'content' => '【该消息涉嫌违规，已被屏蔽】'
            ])
        ];
    }

    /**
     * 返回指定信息
     */
    function responseImageJson($legal = true, $pass = 1, $content = [])
    {
        if (!$legal) {
            //违规图片替换
            $content['ext_info']['width'] = 600;
            $content['ext_info']['height'] = 662;
            $content['ext_info']['image'] = $content['ext_info']['thumbnail'] = AliyunUtil::IMG_HOST . 'weigui.jpeg';
            $content['ext_info']['localityImage'] = $content['ext_info']['name'] = '';
        }
        return [
            'pass' => $pass,
            'replaceContent' => $legal ? '' : json_encode([
                'ext_info' => $content['ext_info']
            ])
        ];
    }

}
